#!/usr/bin/env python3
# -*- coding: utf-8 -*
import os
import re
import json
import copy
import numpy as np
import sys 
import argparse

sys.path.append("..")
from package.utility import find_targets
from package.utility import ensure_exists
from package.utility import find_sub_targets


def make_parser(): 
    NCPU=os.cpu_count()
    parser = argparse.ArgumentParser(
        prog="jxpamg-predo-merge-pid-timerdata",
        description="jxpamg timer data preprocess script, merge pid's timer data to a single json.")
    parser.add_argument(
            "LOGDIR",
            metavar="LOGDIR",
            help="Destination log directories."
        )
    # parser.add_argument(
    #         "-p", "--proc",
    #         dest="num_proc",
    #         default=int(NCPU/2),
    #         help="Number of processes used to run the task, default:%(default)s"
    #     )
    return parser

def InputdirPath(topdir): #输入一个算例文件夹名，输出一个存着文件夹地址的list
    "遍历文件夹得到文件夹地址"
    topdir = os.path.abspath(topdir)
    ensure_exists(topdir)

    print(f">> scan runcase under {topdir}") 
    runcases = find_targets(topdir, "[0-9]*T[0-9]*", suffix="-xtype d") 
    runcase_num = len(runcases)
    print(f">> {runcase_num} runcases found!")
    return runcases

def FindprogramPath(topdir): #输入一个算例文件夹名，输出一个存着文件夹地址的list
    "遍历文件夹得到文件夹地址"
    topdir = os.path.abspath(topdir)
    ensure_exists(topdir)

    print(f"\t>> scan runcase under {topdir}") 
    runcases = find_sub_targets(topdir, "[a-zA-Z0-9]*", suffix="-xtype d") 
    runcase_num = len(runcases)
    print(f"\t>> {runcase_num} program found!")
    return runcases

def double(matched):
    value = int(matched.group('value'))
    return str(value + 1)

def arraytonum(my_array) :
    "输入一个numpy数组，输出一个去掉最大值、最小值和与平均值误差有50%数据的平均值"
    max_num = my_array.max()
    min_num = my_array.min()
    if min_num != max_num :
        my_array = np.delete(my_array, np.where(my_array == max_num))
    max_num = my_array.max()
    min_num = my_array.min()
    if min_num != max_num :
        my_array = np.delete(my_array, np.where(my_array == min_num))
    mean_num = my_array.mean()
    sup_num = 1.5 * mean_num
    inf_num = 0.5 * mean_num
    for i in my_array :
        if i > sup_num or i < inf_num :
            my_array = np.delete(my_array, np.where(my_array == i))
    if len(my_array) != 0 :
        mean_num = my_array.mean()
    else :
        #TODO:这里想个其它标记
        mean_num = mean_num
    return mean_num


def getaverage(my_data,id_num,length) :#在这里my_data是要处理的数据，id_num是同个接口有几组数据，length是指有多少个接口
    "得到去除最大、最小和离平均值有50%误差的数据后的平均值"
    my_data_tavg = my_data.reshape(id_num,length)#构建二维numpy数组
    mean_data = []
    for i in range(0,length,1) :
        temp_col = my_data_tavg[:,i]
        if len(temp_col) != 0 :
            mean_data.append(arraytonum(temp_col))
        else :
            print(i)
    return mean_data

def makefile_part(mean_data,mix_data,max_data,my_data_tag,level_num,length) :#my_data_tag依序存储的是各个接口的
                                                                        #名字，level_num是各个接口的层级，length是全部接口的数量
    "构建部分最终想要的数据，未包含Setup、Solve、总求解时间和各部件的child"
    pre_level = 0
    pre_tag = []
    pkg = {}
    for i in range(0, length, 1):
        tag = my_data_tag[i].split("_")
        if pre_level == level_num[i] and pre_tag[pre_level - 1] == tag[level_num[i] - 1]: #如果是同一个接口就把时间合起来
            pkg[tag[level_num[i] - 1]]["tmin"] += mix_data[i]
            pkg[tag[level_num[i] - 1]]["tmax"] += max_data[i]
            pkg[tag[level_num[i] - 1]]["tave"] += mean_data[i]
        else:
            if tag[level_num[i] - 2] == "Residuan" : 
                pkg[tag[level_num[i] - 1]] = {"level": level_num[i] - 1,
                                              "root": tag[0],
                                              "parent": tag[level_num[i] - 3],
                                              "tmin": mix_data[i],
                                              "tmax": max_data[i],
                                              "tave": mean_data[i],
                                              "child": []}
            else :
                pkg[tag[level_num[i] - 1]] = {"level": level_num[i],
                                            "root": tag[0],
                                            "parent": tag[level_num[i] - 2],
                                            "tmin": mix_data[i],
                                            "tmax": max_data[i],
                                            "tave": mean_data[i],
                                            "child": []}
            pre_level = level_num[i]
            pre_tag = tag
    return pkg


def getdata2numpy(mydir) :#通过mydir读入json文件然后把想要的数据提取出来
    "把数据存到numpy中方便统计"
    #NOTE:这部分逻辑很怪
    #1.循环次数最多10次，次数多了怎么办？不通用的接口应该标出来
    #2.应该去找当前文件夹下的reapet*timers.json
    str0 = "/reapet0timers.json"
    my_data_tag = []
    my_data_tavg = np.empty((0))
    my_data_min = np.empty((0))
    my_data_max = np.empty((0))
    level_num = []
    id_num = 0
    for id in range(0,10,1) :
        str1 = str0
        str0 = mydir + str0
        if os.path.isfile(str0) :
            fo = open(str0,"r")
            jsonData = fo.read()
            text = json.loads(jsonData)
            for i in text :
                my_data_temp1 = [text[i]["tavg"]]
                my_data_temp2 = [text[i]["tmix"]]
                my_data_temp3 = [text[i]["tmax"]]
                if i not in my_data_tag :
                    my_data_tag.append(i)#记录接口名字
                    level_num.append(text[i]["level"])#记录层级level
                my_data_tavg = np.concatenate([my_data_tavg, my_data_temp1])#存储numpy数据
                my_data_min = np.concatenate([my_data_min, my_data_temp2])
                my_data_max = np.concatenate([my_data_max, my_data_temp3])
            id_num += 1
            fo.close()
        str0 = re.sub('(?P<value>\d+)', double, str1)
    length = len(my_data_tag)
    my_data_tavg = getaverage(my_data_tavg,id_num,length)#得到平均
    my_data_max = getaverage(my_data_max,id_num,length)
    my_data_min = getaverage(my_data_min,id_num,length)
    return makefile_part(my_data_tavg,my_data_min,my_data_max,my_data_tag,level_num,length)

def makefile_part2(pkg,mean_data,min_data,max_data,my_data_tag) :#完善最后输出的数据格式
    "构建Setup、Solve、总求解时间和各部件的child"
    pkg["Setup"] = {    "level": 1,
                        "parent": "TotalSolveTime",
                        "tmin": pkg["CreateS"]["tmin"],
                        "tmax": pkg["CreateS"]["tmax"],
                        "tave": pkg["CreateS"]["tave"],
                        "child": []}
    pkg["Setup"]["tmin"] = pkg["Setup"]["tmin"] + pkg["Coarsen"]["tmin"]   
    pkg["Setup"]["tmax"] = pkg["Setup"]["tmax"] + pkg["Coarsen"]["tmax"]
    pkg["Setup"]["tave"] = pkg["Setup"]["tave"] + pkg["Coarsen"]["tave"]
    pkg["Setup"]["tmin"] = pkg["Setup"]["tmin"] + pkg["BuildInterp"]["tmin"]   
    pkg["Setup"]["tmax"] = pkg["Setup"]["tmax"] + pkg["BuildInterp"]["tmax"]
    pkg["Setup"]["tave"] = pkg["Setup"]["tave"] + pkg["BuildInterp"]["tave"]
    pkg["Setup"]["tmin"] = pkg["Setup"]["tmin"] + pkg["RAP"]["tmin"]   
    pkg["Setup"]["tmax"] = pkg["Setup"]["tmax"] + pkg["RAP"]["tmax"]
    pkg["Setup"]["tave"] = pkg["Setup"]["tave"] + pkg["RAP"]["tave"]
    pkg["Solve"] = {"level": 1,
                        "parent": "TotalSolveTime",
                        "tmin": min_data[1],
                        "tmax": max_data[1],
                        "tave": mean_data[1],
                        "child": []}
    pkg["TotalSolveTime"] = {"level": 0,
                            "parent": "None",
                            "tmin": pkg["Solve"]["tmin"] + pkg["Setup"]["tmin"],#min_data[2],
                            "tmax": pkg["Solve"]["tmax"] + pkg["Setup"]["tmax"],#max_data[2],
                            "tave": pkg["Solve"]["tave"] + pkg["Setup"]["tave"],#mean_data[2],
                            "child": []}
    for i in pkg :#完善每个接口的child信息
        if pkg[i]["parent"] != "None" :
            if i not in pkg[pkg[i]["parent"]]["child"] :
                pkg[pkg[i]["parent"]]["child"].append(i)
    return pkg

def loadoutjson(pkg , mydir) :#该接口作用与getdata2numpy一样，再写一次的原因是reapet和out两者的输出格式有所差异
    "读入srun_out.json文件然后在pkg中添加Solve、Setup和总求解时间的数据"
    if len(pkg) == 0 :#遇到错误文件srun_out.json为空的，就跳过这一步
        return "None"
    str0 = mydir + "/srun_out.json"
    fo = open(str0,"r")
    jsonData = fo.read()
    text = json.loads(jsonData)
    my_data_tag = []
    my_data_tavg = np.empty((0))
    my_data_min = np.empty((0))
    my_data_max = np.empty((0))
    for i in text :
        str1 = i.split("_");
        my_data_temp1 = [float(text[i]["tave"])]
        my_data_temp2 = [float(text[i]["tmin"])]
        my_data_temp3 = [float(text[i]["tmax"])]
        if i not in my_data_tag :
            my_data_tag.append(str1[1])
        my_data_tavg = np.concatenate([my_data_tavg, my_data_temp1])
        my_data_min = np.concatenate([my_data_min, my_data_temp2])
        my_data_max = np.concatenate([my_data_max, my_data_temp3])
    id_num = int(len(text) / 3)
    fo.close()
    my_data_tavg = getaverage(my_data_tavg,id_num,3)
    my_data_max = getaverage(my_data_max,id_num,3)
    my_data_min = getaverage(my_data_min,id_num,3)
    return makefile_part2(pkg,my_data_tavg,my_data_min,my_data_max,my_data_tag)

def Output(pkg,mydir,testname) :#把文件输出到runcase层级的文件夹下
    "输出结果"   
    pkg = json.dumps(pkg, sort_keys=True, indent=4, separators=(',', ': '))
    str0 = mydir + f"/{testname}-database.json" #输出文件名待确定，这里是随便打了一个测试用的
    print(str0)
    fw=open(str0,"w")
    fw.write(pkg)
    fw.close()
    return

def main(mydir) :
#    Input(dir_list)    

    str0 = "0"
    pkg_final = {}
    testname = ""
    my_dir = InputdirPath(mydir)
    for i in my_dir :
        fo = open(i + "/srun_config.json")#获取进程数
        jsonData = fo.read()
        text = json.loads(jsonData)
        proc_num = text["proc_num"]
        #NOTE:这个判断的必要性是？检查输出文件是否有问题？
        #写法上用split，然后判断是否在列表中不是更简单？
        str1 = os.path.split(i)
        while(str1[1] != text["testcase"]) :
            str2 = str1[0]
            str1 = os.path.split(str2)
        str1 = str2
        ################end###############
        #NOTE:下面的步骤没理解，为什么要先Output一次？初始化？感觉python中没必要
        if str1 != str0 and str0 != "0" :
            Output(pkg_final,str0, testname)
            pkg_final = {}
        str0 = str1
        ################end###############
        testname =text["testcase"]
        pkg = loadoutjson(getdata2numpy(i),i)
        if pkg != "None" :
            pkg_final[text["testcase"] + "-np" + str(proc_num)] = pkg
        fo.close()
    Output(pkg_final,str0, text["testcase"])
    


# below add by mrz
# Time: 23-2-22

def writedict2json(dict):
    print("\nwrite data into:")
    for k in dict:
        programdict = dict[k]
        for i in programdict:
            dir = programdict[i].pop("dir")
            jsondata = json.dumps(programdict[i], sort_keys=True, indent=4, separators=(',', ': '))
            with open(f"{dir}/{k}-{i}-database.json","w") as fo:
                fo.write(jsondata)
            print(f"  {dir}/{k}-{i}-database.json")
    return

def mergenpdict(dict1,dict2):
    for i in dict2:
        if i not in dict1:
            dict1[i] = dict2[i]
        else:
            dict1[i]["tavg"] = (dict1[i]["tavg"] + dict2[i]["tavg"]) / 2
            dict1[i]["tmax"] = (dict1[i]["tmax"] + dict2[i]["tmax"]) / 2
            dict1[i]["tmin"] = (dict1[i]["tmin"] + dict2[i]["tmin"]) / 2

    return dict1

def initTimedict():
    dict = {}
    dict["tavg"] = 0
    dict["tmax"] = 0
    dict["tmin"] = 0
    return dict

def addicttime(dict1,dict2):
    for m in ["tavg", "tmin", "tmax"]:
        dict1[m] += dict2[m]
    return dict1

def getTotalTime(dict):
    Totaltime = initTimedict()
    Setuptime = initTimedict()
    Solvetime = initTimedict()
    for i in dict:
        # for k in ["avg", "min", "max"]:
        #     Totaltime[k] += dict[i][k]
        Totaltime = addicttime(Totaltime,dict[i])
        if dict[i]["parent"] == "Setup":
            # for k in ["avg", "min", "max"]:
            #     Setuptime[k] += dict[i][k]
            Setuptime = addicttime(Setuptime,dict[i])
        elif dict[i]["parent"] == "Solve":
            # for k in ["avg", "min", "max"]:
            #     Solvetime[k] += dict[i][k]
            Solvetime = addicttime(Solvetime,dict[i])
    dict["SetupTime"] = Setuptime
    dict["SolveTime"] = Solvetime
    dict["Totaltime"] = Totaltime
    return dict

def getchild(dict):
    for i in dict:
        parent = dict[i]["parent"]
        root   = dict[i]["root"]
        if len(parent) > 0:
            if f"{root}-{parent}" in dict:
                dict[f"{root}-{parent}"]["child"].append(i)
                dict[i]["parent"] = f"{root}-{parent}"
            elif parent in dict:
                dict[parent]["child"].append(i)
    return dict

def changedictkey(dict):
    """将不同的key划分为关键信息存入字典中"""
    newdict = {}
    for i in dict:
        #split key
        keylist = i.split("_")
        tagnumber = -1
        for j in keylist:
            if re.compile(r".*[0-9]$").match(j):
                # print(j)
                continue
            else:
                tagnumber+=1
            # if j.isalpha():
            #     tagnumber+=1

        #write into new key
        newkey = keylist[tagnumber]
        if newkey=="Norm":
            newkey="ResiduanNorm"
            keylist[tagnumber] = newkey
            del keylist[tagnumber-1]
            tagnumber = tagnumber - 1
            dict[i]["level"] = 2
            
        #TODO:代码版本问题，需要在新版本中删除
        elif keylist[0] == "Relax":
            keylist.insert(0,"Solve")
            tagnumber+=1
        elif keylist[0] == "Restrict":
            keylist.insert(0,"Solve")
            tagnumber+=1
        elif keylist[0] == "Restrict":
            keylist.insert(0,"Prolong")
            tagnumber+=1

        # 目的是加入 AMGLevelTime AMGCycleLevelTime 两个新的数组
        # 1.记录最大Level和Cycle
        # 2.Time形成一个新数组
        AmgCycle = -1
        AmgLevel = -1
        Relaxtag = -1
        for l in range(tagnumber+1, len(keylist)):
            # print(keylist[l])
            tmpsplitlist = re.findall(r'[0-9]+|[a-zA-Z]+',keylist[l])
            # print(tmpsplitlist)
            if(tmpsplitlist[0] == "C"):
                AmgCycle = int(tmpsplitlist[1])
            elif(tmpsplitlist[0] == "l"):
                AmgLevel = int(tmpsplitlist[1])
            elif(tmpsplitlist[0] == "Relaxtag"):
                Relaxtag = int(tmpsplitlist[1])
        
        #这里解决SpMV在不同地方重复出现的问题
        #解决方法就是把父接口加入tag名字
        if newkey in newdict:
            if newkey.startswith("SpMV"):
                newkey = f"{keylist[0]}{keylist[1]}-{newkey}"
            elif (tagnumber - 1 >= 0) and (newdict[newkey]["root"] != keylist[0] or newdict[newkey]["parent"] != keylist[tagnumber - 1]) :
                # 注释代码打算将原有的Tag修改，但这明显不符合判断逻辑
                # oldkey_parent = newdict[newkey]["parent"]
                # newdict[f"{oldkey_parent}-{newkey}"] = newdict.pop(newkey)
                newkey = f"{keylist[0]}-{newkey}"
                
            # print(i,newkey)

        #for debug
        # if(newkey.startswith("Relax")):
        #     print(i,newkey,AmgCycle)
        # if(newkey.startswith("Restrict")):
        #     print(i,newkey,AmgCycle)
        # print(i,newkey,AmgLevel)

        if newkey not in newdict:
            newdict[newkey] = {}
            if tagnumber - 1 >= 0: 
                newdict[newkey]["parent"] = keylist[tagnumber - 1]
            else:
                newdict[newkey]["parent"] = ""
            newdict[newkey]["child"] = []
            newdict[newkey]["root"] = keylist[0]
            if(AmgCycle >= 0):
                newdict[newkey]["AmgCycle"] = AmgCycle
            if(AmgLevel >= 0):
                newdict[newkey]["AmgLevel"] = AmgLevel
            if(Relaxtag >= 0):
                newdict[newkey]["Relaxtag"] = Relaxtag
            # 初始化 TimeLevel
            newdict[newkey]["TimeLevel"] = {}
            # 复制dict
            for k in dict[i]:
                newdict[newkey][k] = dict[i][k]
        else:
            if(AmgCycle >= 0):
                newdict[newkey]["AmgCycle"] = max(AmgCycle,newdict[newkey]["AmgCycle"])
            if(AmgLevel >= 0):
                newdict[newkey]["AmgLevel"] = max(AmgLevel,newdict[newkey]["AmgLevel"])
            if(Relaxtag >= 0):
                newdict[newkey]["Relaxtag"] = max(Relaxtag,newdict[newkey]["Relaxtag"])
            # newdict[newkey]["tmin"] += dict[i]["tmin"]
            # newdict[newkey]["tmax"] += dict[i]["tmax"]
            # newdict[newkey]["tavg"] += dict[i]["tavg"]
            newdict[newkey] = addicttime(newdict[newkey],dict[i])
        #生成leveldict
        leveldict = {}   #NOTE:leveldict会利用Tag时的一些特点，比如Relaxtag前一定有Cycle和level
        """
        "Cycle_*":{
            level_*:{
            #NOTE:Relaxtag每层操作完全统一，目前不统计
                Relaxtag_*:{
                tmin:
                tmax:
                tavg:
                }
            }
        }
        有的可能不存在，很难写成通用性
        根据目前已有情况判断：
        1.level_*
        2.level_*_Cycle_*
        3.level_*_Cycle_*_RelaxTag_*(第三种合并为第二种)
        """
        if(AmgLevel >= 0 and AmgCycle < 0):
            leveldict[f"level_{AmgLevel}"] = {}
            leveldict[f"level_{AmgLevel}"]["tmin"] = dict[i]["tmin"] 
            leveldict[f"level_{AmgLevel}"]["tmax"] = dict[i]["tmax"] 
            leveldict[f"level_{AmgLevel}"]["tavg"] = dict[i]["tavg"]
            #leveldict 复制到 newdict中
            if f"level_{AmgLevel}" not in newdict[newkey]["TimeLevel"]:
                newdict[newkey]["TimeLevel"][f"level_{AmgLevel}"] = leveldict[f"level_{AmgLevel}"]
        elif(AmgLevel < 0 and AmgCycle >= 0):
            leveldict[f"cycle_{AmgCycle}"] = {}
            leveldict[f"cycle_{AmgCycle}"]["tmin"] = dict[i]["tmin"] 
            leveldict[f"cycle_{AmgCycle}"]["tmax"] = dict[i]["tmax"] 
            leveldict[f"cycle_{AmgCycle}"]["tavg"] = dict[i]["tavg"]
            #leveldict 复制到 newdict中
            if f"cycle_{AmgCycle}" not in newdict[newkey]["TimeLevel"]:
                newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"] = leveldict[f"cycle_{AmgCycle}"]
        elif(AmgLevel >= 0 and AmgCycle >= 0 and Relaxtag < 0):
            leveldict[f"cycle_{AmgCycle}"] = {}
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"] = {}
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmin"] = dict[i]["tmin"]
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmax"] = dict[i]["tmax"]
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tavg"] = dict[i]["tavg"]
            #leveldict 复制到 newdict中
            if f"cycle_{AmgCycle}" not in newdict[newkey]["TimeLevel"]:
                newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"] = leveldict[f"cycle_{AmgCycle}"]
            else:
                newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"][f"level_{AmgLevel}"] = leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]
        elif(AmgLevel >= 0 and AmgCycle >= 0 and Relaxtag >= 0 ):
            leveldict[f"cycle_{AmgCycle}"] = {}
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"] = {}
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmin"] = dict[i]["tmin"]
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmax"] = dict[i]["tmax"]
            leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tavg"] = dict[i]["tavg"]
            #leveldict 复制到 newdict中
            if f"cycle_{AmgCycle}" not in newdict[newkey]["TimeLevel"]:
                newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"] = leveldict[f"cycle_{AmgCycle}"]
            else:
                if f"cycle_{AmgLevel}" not in newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"]:
                    newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"][f"level_{AmgLevel}"] = leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]
                else:
                    newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmin"] += leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmin"]
                    newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmax"] += leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tmax"]
                    newdict[newkey]["TimeLevel"][f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tavg"] += leveldict[f"cycle_{AmgCycle}"][f"level_{AmgLevel}"]["tavg"]
    # print(newdict)
    return newdict



def getdictave(dict):
    """将字典中元素处理得到去除最大、最小和离平均值有50%误差的数据后的平均值"""
    for i in dict:
        for j in dict[i]:
            if isinstance(dict[i][j],list):
                dict[i][j] = arraytonum(np.array(dict[i][j]))
    return dict

def getaveragetime(reapetpath):
    """将不同reapet汇聚，并处理为一个数据"""
    mergedict = {}

    reapetfile = find_targets(reapetpath, "reapet*timers.json", suffix="-xtype f")

    for i in reapetfile:
        if(len(i) == 0):
            break
        with open(i, 'r') as fo:
            jsonData = fo.read()
            text = json.loads(jsonData)
            for i in text :
                #key相同的数据形成一个列表
                if i not in mergedict:
                    mergedict[i] = {}
                    mergedict[i]["level"] = text[i]["level"]
                    mergedict[i]["tavg"]  = [text[i]["tavg"]]
                    mergedict[i]["tmax"]  = [text[i]["tmax"]]
                    mergedict[i]["tmin"]  = [text[i]["tmix"]]
                else:
                    mergedict[i]["tavg"].append(text[i]["tavg"])
                    mergedict[i]["tmax"].append(text[i]["tmax"])
                    mergedict[i]["tmin"].append(text[i]["tmix"])
    # print(mergedict)
    mergedict = getdictave(mergedict)
    # print(mergedict)
    return mergedict

def test(dirpath):
    Inpttdirlist = InputdirPath(dirpath)
    resultdict   = {}   
    for i in Inpttdirlist :
        programlist = FindprogramPath(i)
        for j in programlist:
            programname = os.path.basename(j)
            if programname not in resultdict:
                resultdict[programname] = {}
            mergepiddict = resultdict[programname] 
            case_config = find_targets(i, "*info.json", suffix="-xtype f")[0]
            with open(f"{case_config}", 'r') as fo:
                jsonData = fo.read()
                text = json.loads(jsonData)
                proc_num = text["argument"]["numprocess"]
                casename = text["casename"]
                if casename not in mergepiddict:
                    mergepiddict[casename] = {}
                    mergepiddict[casename]["dir"] = dirpath
                    # print(casename)
                    print(j)
            tmpdict = getaveragetime(j)
            tmpdict = changedictkey(tmpdict)
            tmpdict = getchild(tmpdict)
            tmpdict = getTotalTime(tmpdict)

            #以防后续出现重复的进程数
            if f"np_{proc_num}" in mergepiddict[f"{casename}"]:
                mergepiddict[f"{casename}"][f"np_{proc_num}"] = mergenpdict(mergepiddict[f"{casename}"][f"np_{proc_num}"], tmpdict)
            else:
                mergepiddict[f"{casename}"][f"np_{proc_num}"] = tmpdict
            #print(mergepiddict[f"{casename}-np{proc_num}"])
    writedict2json(resultdict)


if __name__=="__main__":
    parser = make_parser()
    args = parser.parse_args()
    # main(mydir)
    test(args.LOGDIR)
